﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Collections;
using DevExpress.XtraTreeList.Nodes;

namespace TS_XY_Train_Repaire.UserCon.Locomotive.TrainRepair.Work.WorkPub
{
    public partial class uc_Locomotive_TrainRepair_Work_WorkPub_Group_Pub : DevExpress.XtraEditors.XtraForm
    {
        public string _BMWID = "";
        public string _LoadType = "0";
        public uc_Locomotive_TrainRepair_Work_WorkPub_Group_Pub()
        {
            InitializeComponent();
        }

        private void uc_Locomotive_TrainRepair_Work_WorkPub_Group_Pub_Load(object sender, EventArgs e)
        {
            TS_Win_Class.FillListTypeAct(flp_Dep, "select f_id,f_name,0 as f_count from TS_S_T_P_DepartMent where f_id in (select f_depid  from TS_S_T_B_WorkMain_Dispatcher where f_type="+_LoadType+" and f_del=0)");
            string strSQL = "select f_id,f_state,f_pstime,f_petime,(select f_ttname+f_name+' '+CONVERT(varchar(100), f_stime, 23) from TS_TR_BU_Work_Main where f_id=TS_TR_BU_Work_Main_Steps.f_bmwid)+' '+f_name as f_name from TS_TR_BU_Work_Main_Steps where f_id=" + _BMWID;
            SqlConnection conn = new SqlConnection(TS_Win_Class.GetSQlConnStr());
            conn.Open();
            SqlCommand comm = new SqlCommand(strSQL, conn);
            SqlDataReader read = comm.ExecuteReader();
            SqlConnection conn_s = new SqlConnection(TS_Win_Class.GetSQlConnStr());
            while (read.Read())
            {
                this.Text = read["f_name"].ToString();
                de_S.Text = read["f_pstime"].ToString();
                de_E.Text = read["f_petime"].ToString();
            }
            read.Close();
            conn.Close();
            conn.Dispose();
            _LoadGroupList();
            _Load_Dep("-1");
            _Load_Stuff2("-1");
        }

        private void uc_Locomotive_TrainRepair_Work_WorkPub_Group_Pub_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.DialogResult = DialogResult.OK;
        }
        private void _LoadGroupList()
        {
            string strSQL = "";
            if (cb_0.Checked)
            {
                bt_Pub.Enabled = true;
                bt_Del.Enabled = false;
                strSQL = "select convert(bit,0) as f_ck,f_id,f_name,f_bmwdid,convert(nvarchar(16),f_pstime,120) as f_pstime,convert(nvarchar(16),f_petime,120) as f_petime from TS_TR_BU_Work_Main_Group where f_del=0 and f_id not in(select f_rid from TS_TR_BU_Work_Main_Relation where f_rtype=3 and f_del=0) and f_bmwsid=" + _BMWID;
            }
            if (cb_1.Checked)
            {
                strSQL = "select convert(bit,0) as f_ck,f_id,f_name,f_bmwdid,convert(nvarchar(16),f_pstime,120) as f_pstime,convert(nvarchar(16),f_petime,120) as f_petime from TS_TR_BU_Work_Main_Group where f_del=0 and f_id in(select f_rid from TS_TR_BU_Work_Main_Relation where f_rtype=3 and f_del=0) and f_bmwsid=" + _BMWID;
                bt_Pub.Enabled = false;
                bt_Del.Enabled = true;
            }
            TR_PS_Work_Main_List _tv = new TR_PS_Work_Main_List();
            ArrayList NList = new ArrayList();
            SqlConnection conn = new SqlConnection(TS_Win_Class.GetSQlConnStr());
            conn.Open();
            SqlCommand comm = new SqlCommand(strSQL, conn);
            SqlDataReader read = comm.ExecuteReader();
            while (read.Read())
            {
                _tv = new TR_PS_Work_Main_List();
                _tv.f_id = read["f_id"].ToString();
                //_tv.f_parent = "D" + read["f_bmwdid"].ToString();
                _tv.f_name = read["f_name"].ToString();
                _tv.f_stime = read["f_pstime"].ToString();
                _tv.f_etime = read["f_petime"].ToString();
                _tv.f_ck = (Boolean)read["f_ck"];
                NList.Add(_tv);
            }
            read.Close();
            conn.Close();
            conn.Dispose();
            xtt_NoPub.DataSource = NList;
            xtt_NoPub.ExpandAll();
        }
        private void _Load_Dep(string _GID)
        {
            flp_Dep.Controls.Clear();
            //string strSQL = "select  f_id,f_name,(select count(*) from TS_TR_BU_Work_Main_Group_Dep where f_del=0 and f_depid=TS_S_T_P_Department.f_id and f_bmwgid="+_GID+")as f_count from TS_S_T_P_Department where f_del=0 and f_level like '%,52,%'";
            string strSQL = "select f_id,f_name,(select count(*) from TS_TR_BU_Work_Main_Relation where f_del=0 and f_ftype=0 and f_fid=TS_S_T_P_Stuff.f_id and f_rtype=4 and f_rid=" + _GID + ") as f_count from TS_S_T_P_Stuff where f_del=0 and f_depid=" + TS_Win_Locomotive_MainForm.pUserInfoMain.UserDepIndex;
            SqlConnection conn = new SqlConnection(TS_Win_Class.GetSQlConnStr());
            conn.Open();
            SqlCommand comm = new SqlCommand(strSQL, conn);
            SqlDataReader read = comm.ExecuteReader();
            while (read.Read())
            {
                CheckBox _cb = new CheckBox();
                _cb.Name = "cb_D_" + read["f_id"].ToString();
                _cb.Text = read["f_name"].ToString();
                _cb.Width = 80;
                _cb.Padding = new System.Windows.Forms.Padding(0);
                _cb.Tag = read["f_id"].ToString();
                _cb.Margin = new Padding(0);
                if (read["f_count"].ToString() != "0")
                {
                    _cb.Checked = true;
                }
                flp_Dep.Controls.Add(_cb);
            }
            read.Close();
            read.Dispose();
            comm.Dispose();
            conn.Close();
            conn.Dispose();
        }
        private void _Load_Stuff2(string _GID)
        {
            flp_Stuff2.Controls.Clear();
            //string strSQL = "select  f_id,f_name,(select count(*) from TS_TR_BU_Work_Main_Group_Dep where f_del=0 and f_depid=TS_S_T_P_Department.f_id and f_bmwgid="+_GID+")as f_count from TS_S_T_P_Department where f_del=0 and f_level like '%,52,%'";
            string strSQL = "select f_id,f_name,(select count(*) from TS_TR_BU_Work_Main_Relation where f_del=0 and f_ftype=0 and f_fid=TS_S_T_P_Stuff.f_id and f_rtype=5 and f_rid=" + _GID + ") as f_count from TS_S_T_P_Stuff where f_del=0 and f_depid=" + TS_Win_Locomotive_MainForm.pUserInfoMain.UserDepIndex;
            SqlConnection conn = new SqlConnection(TS_Win_Class.GetSQlConnStr());
            conn.Open();
            SqlCommand comm = new SqlCommand(strSQL, conn);
            SqlDataReader read = comm.ExecuteReader();
            while (read.Read())
            {
                CheckBox _cb = new CheckBox();
                _cb.Name = "cb_D_" + read["f_id"].ToString();
                _cb.Text = read["f_name"].ToString();
                _cb.Width = 80;
                _cb.Padding = new System.Windows.Forms.Padding(0);
                _cb.Tag = read["f_id"].ToString();
                _cb.Margin = new Padding(0);
                if (read["f_count"].ToString() != "0")
                {
                    _cb.Checked = true;
                }
                flp_Stuff2.Controls.Add(_cb);
            }
            read.Close();
            read.Dispose();
            comm.Dispose();
            conn.Close();
            conn.Dispose();
        }
        private void bt_Exit_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
        }

        private void bt_Edit_Click(object sender, EventArgs e)
        {
            string strSQL = "update TS_TR_BU_Work_Main_Steps set f_pstime='" + de_S.Text.ToString() + "', f_petime='" + de_E.Text.ToString() + "',f_time=getdate(),f_workerid=" + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + " where f_bmwdid=" + _BMWID;
            strSQL += ";update TS_TR_BU_Work_Main_Group set f_pstime=(select CONVERT(varchar(100),f_pstime, 23) from TS_TR_BU_Work_Main_Steps where f_id=TS_TR_BU_Work_Main_Group.f_bmwsid) + ' ' +CONVERT(varchar(100), f_pstime, 8),f_petime=(select CONVERT(varchar(100),f_petime, 23) from TS_TR_BU_Work_Main_Steps where f_id=TS_TR_BU_Work_Main_Group.f_bmwsid) + ' ' +CONVERT(varchar(100), f_petime, 8) where f_bmwsid in (select f_id from TS_TR_BU_Work_Main_Steps where f_bmwdid=" + _BMWID + ")";
            TS_Win_Class.MySqlCom(strSQL);
        }

        private void bt_Pub_Click(object sender, EventArgs e)
        {
            string _str = "";
            string _Stuffid = "";
            string _Stuff1id = "";
            for (int i = 0; i < xtt_NoPub.Nodes.Count; i++)
            {
                if ((bool)xtt_NoPub.Nodes[i]["f_ck"])
                {
                    _str += "," + xtt_NoPub.Nodes[i]["f_id"].ToString();
                }
            }
            foreach (Control _cl in flp_Dep.Controls)
            {
                if ((_cl as CheckBox).Checked)
                {
                    _Stuffid += "," + (_cl as CheckBox).Tag.ToString();
                }
            }
            foreach (Control _cl in flp_Stuff2.Controls)
            {
                if ((_cl as CheckBox).Checked)
                {
                    _Stuff1id += "," + (_cl as CheckBox).Tag.ToString();
                }
            }
            if (_str.Length > 0 && _Stuffid.Length > 0 && _Stuff1id.Length > 0)
            {
                _str = _str.Substring(1);
                _Stuffid = _Stuffid.Substring(1);
                _Stuff1id = _Stuff1id.Substring(1);
                string strSQL = "update TS_TR_BU_Work_Main_Group set f_pstime='" + de_S.Text.ToString() + "', f_petime='" + de_E.Text.ToString() + "',f_time=getdate(),f_workerid=" + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + " where f_id in (" + _str + ")";
                TS_Win_Class.MySqlCom(strSQL);
                strSQL = "update TS_TR_BU_Work_Main_Group set f_state=1,f_time=getdate(),f_workerid=" + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + " where f_id in (" + _str + ")";
                TS_Win_Class.MySqlCom(strSQL);
                string[] _bmwgid = _str.Split(',');
                string[] _stuffid = _Stuffid.Split(',');
                string[] _stuff1id = _Stuff1id.Split(',');
                string _WorkPlaceid = TS_Win_Class.GetSQlFieldValue("select f_id from TS_TR_BU_Work_Main_Group_WorkPlace where f_bmwgid in ("+ _str + ")");
                string[] _workplaceid = _WorkPlaceid.Split(',');

                //Group派到人 Place派到人
                strSQL = "update TS_TR_BU_Work_Main_Group set f_stime=getdate() where f_id in (" + _str + ") and f_stime is null";
                TS_Win_Class.MySqlCom(strSQL);
                for (int i = 0; i < _bmwgid.Length; i++)
                {
                    for (int n = 0; n < _stuffid.Length; n++)
                    {
                        strSQL = "insert into TS_TR_BU_Work_Main_Relation(f_type,f_rtype,f_rid,f_ftype,f_fid,f_workerid,f_cstuffid,f_ctime) values("+_LoadType+",3," + _bmwgid[i] + ",0," + _stuffid[n] + "," + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + "," + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + ",getdate())";
                        string _ID = TS_Win_Class.MySqlComGetID(strSQL);
                        strSQL = "insert into TS_S_T_P_Voice_Announce(f_vid,f_tablename,f_string,f_funcid,f_type,f_workerid) values(" + _ID + ",'TS_TR_BU_Work_Main_Relation','" + Create_Voice_String(_bmwgid[i], _stuffid[n],"自检") + "'," + _stuffid[n] + ",0," + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + ")";
                        TS_Win_Class.MySqlCom(strSQL);
                    }
                    for (int n = 0; n < _stuff1id.Length; n++)
                    {
                        strSQL = "insert into TS_TR_BU_Work_Main_Relation(f_type,f_rtype,f_rid,f_ftype,f_fid,f_workerid,f_cstuffid,f_ctime) values(" + _LoadType + ",3," + _bmwgid[i] + ",0," + _stuff1id[n] + "," + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + "," + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + ",getdate())";
                        string _ID = TS_Win_Class.MySqlComGetID(strSQL);
                        strSQL = "insert into TS_S_T_P_Voice_Announce(f_vid,f_tablename,f_string,f_funcid,f_type,f_workerid) values(" + _ID + ",'TS_TR_BU_Work_Main_Relation','" + Create_Voice_String(_bmwgid[i], _stuff1id[n],"互检") + "'," + _stuff1id[n] + ",0," + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + ")";
                        TS_Win_Class.MySqlCom(strSQL);
                    }
                }
                strSQL = "update TS_TR_BU_Work_Main_Group_WorkPlace set f_state=1,f_time=getdate(),f_workerid=" + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + "  where f_del=0 and f_id in(" + _WorkPlaceid+")";
                TS_Win_Class.MySqlCom(strSQL);
                for (int i=0;i< _workplaceid.Length;i++)
                {
                    for (int n = 0; n < _stuffid.Length; n++)
                    {
                        strSQL = "insert into TS_TR_BU_Work_Main_Relation(f_type,f_rtype,f_rid,f_ftype,f_fid,f_workerid,f_cstuffid,f_ctime) values(" + _LoadType + ",4," + _workplaceid[i] + ",0," + _stuffid[n] + "," + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + "," + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + ",getdate())";
                        string _ID = TS_Win_Class.MySqlComGetID(strSQL);
                        strSQL = "insert into TS_S_T_P_Voice_Announce(f_vid,f_tablename,f_string,f_funcid,f_type,f_workerid) values(" + _ID + ",'TS_TR_BU_Work_Main_Relation','" + Create_Voice_String_1(_workplaceid[i], _stuffid[n], "自检") + "'," + _stuffid[n] + ",0," + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + ")";
                        TS_Win_Class.MySqlCom(strSQL);
                    }
                    for (int n = 0; n < _stuff1id.Length; n++)
                    {
                        strSQL = "insert into TS_TR_BU_Work_Main_Relation(f_type,f_rtype,f_rid,f_ftype,f_fid,f_workerid,f_cstuffid,f_ctime) values(" + _LoadType + ",5," + _workplaceid[i] + ",0," + _stuff1id[n] + "," + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + "," + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + ",getdate())";
                        string _ID = TS_Win_Class.MySqlComGetID(strSQL);
                        strSQL = "insert into TS_S_T_P_Voice_Announce(f_vid,f_tablename,f_string,f_funcid,f_type,f_workerid) values(" + _ID + ",'TS_TR_BU_Work_Main_Relation','" + Create_Voice_String_1(_workplaceid[i], _stuff1id[n], "互检") + "'," + _stuff1id[n] + ",0," + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + ")";
                        TS_Win_Class.MySqlCom(strSQL);
                    }
                }
                _LoadGroupList();
            }
            else
            {
                MessageBox.Show("请选择正确的作业组和作业人员！");
            }
        }
        private string Create_Voice_String(string _RID, string _DepID,string _Type)
        {
            string _str = "";
            string strSQL = "select '机车号'+(select f_ttname+f_name from TS_TR_BU_Work_Main where f_id=TS_TR_BU_Work_Main_Group.f_bmwid)+','+f_name as f_name,f_pstime,f_petime from TS_TR_BU_Work_Main_Group where f_id=" + _RID;
            string[] _Result;
            _Result = TS_Win_Class.GetSQlFieldValue(strSQL, "f_name,f_pstime,f_petime".Split(','));
            _str = _Result[0];
            _str += ",计划开始作业时间:" + _Result[1];
            _str += ",计划作业结束时间:" + _Result[2];
            strSQL = "select f_name from TS_S_T_P_Stuff where f_id in (" + _DepID + ")";
            _str += ",任务内容已经发布，";
            SqlConnection conn = new SqlConnection(TS_Win_Class.GetSQlConnStr());
            conn.Open();
            SqlCommand comm = new SqlCommand(strSQL, conn);
            SqlDataReader read = comm.ExecuteReader();
            if (read.HasRows)
            {
                _str += _Type+"作业者为：";
                while (read.Read())
                {
                    _str += read["f_name"].ToString();
                }
            }
            else
            {
                _str += "尚未指定作业者！";
            }
            read.Close();
            read.Dispose();
            comm.Dispose();
            conn.Close();
            conn.Dispose();
            return _str;
        }
        private string Create_Voice_String_1(string _RID, string _DepID, string _Type)
        {
            string _str = "";
            string strSQL = "select '机车号'+(select f_ttname+f_name from TS_TR_BU_Work_Main where f_id=TS_TR_BU_Work_Main_Group_WorkPlace.f_bmwid)+','+f_name as f_name,(select f_pstime from TS_TR_BU_Work_Main_Group where f_id=TS_TR_BU_Work_Main_Group_WorkPlace.f_bmwgid)as f_pstime,(select f_petime from TS_TR_BU_Work_Main_Group where f_id=TS_TR_BU_Work_Main_Group_WorkPlace.f_bmwgid) as f_petime from TS_TR_BU_Work_Main_Group_WorkPlace where f_id=" + _RID;
            string[] _Result;
            _Result = TS_Win_Class.GetSQlFieldValue(strSQL, "f_name,f_pstime,f_petime".Split(','));
            _str = _Result[0];
            _str += ",计划开始作业时间:" + _Result[1];
            _str += ",计划作业结束时间:" + _Result[2];
            strSQL = "select f_name from TS_S_T_P_Stuff where f_id in (" + _DepID + ")";
            _str += ",任务内容已经发布，";
            SqlConnection conn = new SqlConnection(TS_Win_Class.GetSQlConnStr());
            conn.Open();
            SqlCommand comm = new SqlCommand(strSQL, conn);
            SqlDataReader read = comm.ExecuteReader();
            if (read.HasRows)
            {
                _str += _Type + "作业者为：";
                while (read.Read())
                {
                    _str += read["f_name"].ToString();
                }
            }
            else
            {
                _str += "尚未指定作业者！";
            }
            read.Close();
            read.Dispose();
            comm.Dispose();
            conn.Close();
            conn.Dispose();
            return _str;
        }

        private void cb_0_CheckedChanged(object sender, EventArgs e)
        {
            _LoadGroupList();
        }

        private void cb_2_CheckedChanged(object sender, EventArgs e)
        {
            if (cb_2.Checked)
            {
                cb_2.Text = "全取消";
                foreach (TreeListNode node in xtt_NoPub.Nodes)
                {
                    node.SetValue("f_ck", true);
                }
            }
            else
            {
                cb_2.Text = "全选";
                foreach (TreeListNode node in xtt_NoPub.Nodes)
                {
                    node.SetValue("f_ck", false);
                }
            }
        }

        private void bt_Del_Click(object sender, EventArgs e)
        {
            string _str = "";
            for (int i = 0; i < xtt_NoPub.Nodes.Count; i++)
            {
                if ((bool)xtt_NoPub.Nodes[i]["f_ck"])
                {
                    _str += "," + xtt_NoPub.Nodes[i]["f_id"].ToString();
                }
            }
            string strSQL = "";
            if (_str.Length > 0)
            {
                _str = _str.Substring(1);
                strSQL = ";update TS_TR_BU_Work_Main_Group set f_state=0,f_time=getdate(),f_stime=null,f_workerid=" + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + " where f_del=0 and f_id in (" + _str + ")";
                strSQL += ";update TS_TR_BU_Work_Main_Group_WorkPlace set f_state=-1,f_time=getdate(),f_workerid=" + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + " where f_del=0 and f_bmwgid in (" + _str + ")";
                strSQL += ";update TS_TR_BU_Work_Main_Relation set f_del=1,f_time=getdate(),f_workerid=" + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + " where f_rtype=3 and f_rid in (" + _str + ") and f_del=0";
                strSQL += ";update TS_TR_BU_Work_Main_Resource set f_del=1,f_time=getdate(),f_workerid=" + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + " where f_maintype=3 and f_mainid in (" + _str + ") and f_del=0";
                strSQL += ";update TS_TR_BU_Work_Main_Relation set f_del=1,f_time=getdate(),f_workerid=" + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + " where f_rtype in (4,5,6,7,8,9,10) and f_rid in (select f_id from TS_TR_BU_Work_Main_Group_WorkPlace where f_del=0 and f_bmwgid in (" + _str + ")) and f_del=0";
                strSQL += ";update TS_TR_BU_Work_Main_Resource set f_del=1,f_time=getdate(),f_workerid=" + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + " where f_maintype=4 and f_mainid in (select f_id from TS_TR_BU_Work_Main_Group_WorkPlace where f_del=0 and f_bmwgid in (" + _str + ")) and f_del=0";
                TS_Win_Class.MySqlCom(strSQL);
                _LoadGroupList();
            }
        }
    }
}